/* ------------------------------------------------------------------------
	Class: svgallery
	Use: Gallery plugin for jQuery
	Author: Hoang Tran
    Single view gallery (only one image spot with different images each taking place to be displayed
    at a time, but a time interval
    Mainly used for display a node in the list mode
    the thumbnail gallery of the node will be displayed 
	Version: 1.1
    Notation: _myLocalFunction()
            $.fn.svgallery.myPublicFunction()
    See http://www.learningjquery.com/2007/10/a-plugin-development-pattern
    See http://jonraasch.com/blog/a-simple-jquery-slideshow
    
------------------------------------------------------------------------- */

// create closure
(function($) {
    // plugin definition
    $.fn.svgallery = function(options) {
       var opts = $.extend({}, $.fn.svgallery.defaults, options);
       
        // return jquery object
       return this.each(function() {
            // Global variables needed in multiple functions.   
            // but they stay local for each gallery
            var currentImageIndex = 0;
            var $galleryWrapper = $(this);
            var numOfImages = $galleryWrapper.find(".image-list img").size();
            var $activeImage = $galleryWrapper.find('.active-image');
            //dislay the first image first
            $activeImage.attr('src', $galleryWrapper.find(".image-list img:eq(0)").attr('src'));
            if (opts.startMode == 'adsThumbs')
            {
	            var $activeLink = $galleryWrapper.find('.active-link');
	            $activeLink.attr('href', $galleryWrapper.find(".image-list a:eq(0)").attr('href')); 
			}
            function slideSwitch() {
                currentImageIndex ++;
                if (currentImageIndex == numOfImages)
                    currentImageIndex = 0;
                
                //update the active image
                src = $galleryWrapper.find(".image-list img:eq(" + currentImageIndex + ")").attr('src');
                fadeInterval = parseInt(opts.animationSpeed/4);
                if (opts.startMode == 'adsThumbs')
                {
	                h = $galleryWrapper.find(".image-list a:eq(" + currentImageIndex + ")").attr('href');
	                $activeLink.attr('href', h);
                    //with adsThumbs, we have a little effects
                    $activeImage.fadeOut(fadeInterval, function(){
                        $(this).attr('src', src).fadeIn(fadeInterval);
                    });
                    //.attr('src', src).fadeIn(1000);
	            }
                else
                    $activeImage.attr('src', src);
            }

             var playSlideshow;
             $activeImage.load(function()
             {
                 $(this).css({"display" : "inline"});
                 imgHeight = $(this).height();
                 marginTop = parseFloat (($(this).parent().parent().height() - imgHeight)/2);
                 imgWidth = $(this).width();
                 marginLeft = parseFloat (($(this).parent().parent().width() - imgWidth)/2);
                 $(this).css({"margin-top" : marginTop,"margin-left": marginLeft,"border":"2px solid white"});
                
             })
             .trigger('load');
             
             if (opts.startMode == 'nodeThumbs')
             {
	             $activeImage.hover(function() { /* mouseover */
	                  $(this).css({'cursor':'pointer'});
                  	  playSlideshow = setInterval( function(){slideSwitch.apply($galleryWrapper) }, opts.animationSpeed);
	             }, function() { /* mouseout*/
                    	clearInterval(playSlideshow);
	             });
			}
			else if (opts.startMode == 'adsThumbs')
			{
	             $activeImage.hover(function() { /* mouseover */
	                  $(this).css({'cursor':'pointer'});
	                  clearInterval(playSlideshow);
	             }, function() { /* mouseout*/
                      playSlideshow = setInterval( function(){slideSwitch.apply($galleryWrapper) }, opts.animationSpeed);
	             });
	             $activeImage.trigger('mouseout');
			}		             
             
        }); //end .each
    }; //end .fn.svgallery
    
    $.fn.svgallery.defaults = {
        animationSpeed : 1000,// integer 
        startMode: 'nodeThumbs', /* nodeThumbs, adsThumbs*/
    };
})(jQuery);